#include "Tool.hpp"
#include <unordered_set>

using namespace DBServer::Database;

void
CommandPaser::paser(std::string&& command, CommandRequest& request)
{
    int command_size = command.size();
    int index = 0;
    for (int i = 0; i < command_size; ++i)
    {
        if (command[i] != ' ')
            request.command[index] += command[i];
        else 
            ++index;
    }
    if (index == 1) // the commadn without value
        request.is_have_value = false;
    else
        request.is_have_value = true;
}

#if 0
#include <iostream>


void print(CommandFormat& format)
{
    std::cout << "type : " << format.command[0] << std::endl;
    std::cout << "key : " << format.command[1] << std::endl;
    if (format.is_have_value) 
        std::cout << "value : " << format.command[2] << std::endl;
}

int main()
{
    CommandFormat format;
    std::string command("set key value");
    std::string command_("get key");

    CommandPaser::paser(std::move(command), format);
    print(format);
    CommandPaser::paser(std::move(command_), format);
    print(format);
    return 1;
}

#endif


auto 
Translator::int_to_string(int _int) -> std::string
{
    std::string str_int("");
    while (_int > 0)
    {
        str_int += '0' + (_int%10);
        _int /= 10;
    }
    int len = str_int.size();
    char temp = ' ';
    for (int i = 0; i < len/2; ++i)
    {
        temp = str_int[i];
        str_int[i] = str_int[len-i-1];
        str_int[len-i-1] = temp;
    }
    return str_int;
}

auto
Translator::string_to_int(std::string& s) -> int
{
    int _int = 0;
    int mid = 1;
    for (int i = s.size()-1; i >= 0; --i)
    {
        _int += (s[i] - '0') * mid;
        mid *= 10;
    }

    return _int;
}

auto 
Translator::vector_to_string(std::vector<std::pair<std::string, std::string>>& v) -> std::string
{
    std::string ret{""};

    for (auto entry : v)
    {
        ret += entry.first;
        ret += ":";
        ret += entry.second;
        ret += "'\n";
    }
    return ret;
}

auto
Translator::vector_to_string_1(std::vector<std::string>&& v) -> std::string
{
    std::string ret{""};

    for (auto ele : v)
    {
        ret += ele;
        ret += "->";
    }
    ret += "\\nullptr\n";
    return ret;
}

auto
Translator::string_to_double(std::string&& _double, double& __double) -> bool
{
    double _dou = 0.0;
    double _int = 0.0;
    bool add_int = false;
    static std::unordered_set<char> set = {
        '0',
        '1',
        '2',
        '3',
        '4',
        '5',
        '6',
        '7',
        '8',
        '9',
        '.'
    };
    for (char c : _double)
    {
        if (set.find(c) == set.end())
        {
            return false;
        }
        
        if (c == '.') 
        {
            add_int = true;
        } 
        else if (add_int)
        {
            _dou = (_dou + (c - '0')) * 0.1;
        }
        else
        {
            _int = _int * 10 + (c - '0');
        }
    }
    __double = _dou + _int;
    return true;
}